import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import local_get from '@dfeidao/atom-web/local/get';
import fire_to from '@dfeidao/atom-web/msg/fire';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import msg_error from '@dfeidao/atom-web/msgbox/error';
import success from '@dfeidao/atom-web/msgbox/success';
// import whoami from '@dfeidao/atom-web/msg/whoami';
import storage_get from '@dfeidao/atom-web/storage/get';
import a002 from './a002';
// 修改
export default async function a003(fd: IFeidaoAiBrowserComponent) {
	// 客户端类型
	const client_type: string = local_get(fd, "client_type");
	// 功能名称
	let group_number: string = local_get(fd, "group_number");
	let group_name = fd.data.node.querySelector<HTMLSelectElement>('[data-feidao-id="group_numbers"]').selectedOptions[0].innerHTML;
	if (group_name === "未分组") {
		group_name = "";
		group_number = "";
	}
	const formname = fd.data.node.querySelector<HTMLInputElement>('[data-feidao-id="formname"]').value.trim();
	// 初始页面
	const pub_form = local_get(fd, 'pub_form') as { formname: string, package_no: string, package_name: string, package_path: string };
	// 返回回来的页面
	const pub_package = local_get(fd, "pub_package") as { no: string, name: string, file_url: string };
	// 功能控制点
	const original_data = local_get(fd, "original_data") as [];// 原始数据
	const data = local_get(fd, "data") as [];
	if (!formname) {
		msg_error("请填写功能名称");
		return;
	}
	const arr3 = new Array();
	Array.from(original_data).forEach((a: { code: string }) => {
		arr3.push(a.code);
	});
	const arr4 = new Array();
	Array.from(data).forEach((b: { code: string }) => {
		arr4.push(b.code.toString());
	});
	arr3.forEach((v) => {
		if (arr4.indexOf(v) !== -1) {
			arr4.splice(arr4.indexOf(v), 1);
		}
	});
	const inst_data = new Array();
	arr4.forEach((v) => {
		Array.from(data).forEach((b: { code: string }) => {
			if (v === b.code.toString()) {
				inst_data.push(b);
			}
		});
	});
	// 构建参数
	const filter1 = {} as {
		group_name: string;
		group_number: string;
		formname: string;	// 功能名称
		package_no: string; // 页面编号
		package_name: string;// 页面名称”：弹窗7返回参数;
		package_path: string; // 页面地址：弹窗7返回参数;
	};
	const filter2 = {} as {
		del: string[]; // 删除参数
		inst_data: Array<{}>; // 添加参数
	};
	filter1.group_number = group_number;
	filter1.group_name = group_name;

	if (!pub_package) {
		if (pub_form.formname !== formname) {
			filter1.formname = formname;
		}
		filter1.package_no = pub_form.package_no;
		filter1.package_name = pub_form.package_name;
		filter1.package_path = pub_form.package_path;
		if (local_get(fd, "del_data")) {
			filter2.del = local_get(fd, "del_data");
		}
		if (inst_data.length > 0) {
			filter2.inst_data = inst_data;
		}
	} else {
		filter1.package_no = pub_package.no;
		filter1.package_name = pub_package.name;
		filter1.package_path = pub_package.file_url;
		if (local_get(fd, "del_data")) {
			filter2.del = local_get(fd, "del_data");
		}
		if (inst_data.length > 0) {
			filter2.inst_data = inst_data;
		}
	}
	const formid: string = local_get(fd, 'formid');
	// 调用修改服务;
	// const user = await whoami();

	const prid = storage_get('productid');
	const res = await nodejs('function-definition/zj-000004/s003', { filter1, filter2, pub_form_code: data, productid: prid, client_type, formid });
	if (res === "repeat") {
		msg_error("功能名称重复，请重新输入");
	}
	if (res === "fail") {
		msg_error("修改失败");
	}
	if (res === "success") {
		success("修改成功");
		await a002(fd);
		fire_to('zj-000001', 'a002');
	}
}
